﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Data.Api.Db;
using Data.Db;
using Shop.Managers;
using Data.Api.Repositories;
using Data.Repositories;
using Entity.Categories;
using Entity.Products;
using Data.Mappers;

namespace TestsMyShop.Db_Tests
{
    public class DbTestForManagerCategory
    {
        private IDbShopConcreteContext context;

        private IManagerCategory manager;
        private ICategoriesRepository repository;
        private List<Category> category = new List<Category>();

        private IMapper<Entity.Categories.Category, Data.ModelDatabase.Category> mapperCategory = new CategoryMapper();

        [TestFixtureSetUp]
        public void Init()
        {
            context = new ShopContext();

            repository = new CategoriesRepository(context, mapperCategory);

            manager = new ManagerCategory(repository, context);

            category.Add(new Category() { Name = "1", Description = "1" });
            category.Add(new Category() { Name = "2", Description = "2" });
            category.Add(new Category() { Name = "3", Description = "3" });

            foreach (Category cat in category)
                context.Categories.Add(mapperCategory.WrapperEntity(cat));
        }

        [TestFixtureTearDown]
        public void Clear()
        {
            context.Delete();
        }

        [Test]
        public void Should_add_category_to_database()
        {
            // arrange
            Category cat = new Category() { Name = "Test", Description = "Test" };
            manager.Add(cat);

            // assert
            Assert.AreEqual(cat.Description, manager.All.Where(x => x.Name.Contains("Test")).Single().Description);
            Assert.AreEqual(cat.Name, manager.All.Where(x => x.Name.Contains("Test")).Single().Name);

        }

        [Test]
        public void Should_update_category_to_database()
        {
            Category cat = manager.All.Where(i => i.Id == 3).Single();
            cat.Name = "Update";

            manager.Update(cat);

            Assert.AreEqual(cat.Name, manager.All.Where(i => i.Id == 3).Single().Name);
            Assert.AreEqual(cat.Description, manager.All.Where(i => i.Id == 3).Single().Description);
        }

        [Test]
        public void Should_remove_category_to_database()
        {
            // act
            int count = manager.All.Count();
            manager.Remove(2);

            // assert
            Assert.AreEqual(count - 1, manager.All.Count());
        }
    }
}
